package com.seo.service.keyword.memberE.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.seo.mapper.keyword.KeywordGMapper;
import com.seo.mapper.keyword.memberEidt.KeywordGMemberEMapper;
import com.seo.service.keyword.memberE.sdk.KeywordGMemberEService;
import com.seo.utils.DateUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class KeywordGMemberEServiceImpl implements KeywordGMemberEService {

	@Autowired
	private KeywordGMemberEMapper keywordGMemberEMapper;
	
	@Autowired
	private KeywordGMapper keywordGMapper;

	/**
	 * 会员关键词列表
	 */
	public List<Map<String, Object>> getKeyWordsEList(Map<String, Object> pam) {
		log.info("进入关键词列表service：");
		List<Map<String, Object>> keyWordsList = keywordGMemberEMapper.getKeyWordsEList(pam);
		log.info("关键词列表查询数据：" + keyWordsList);
		return keyWordsList;
	}

	@Override
	public XSSFWorkbook exportKeyWord(Map<String, Object> pam, HttpServletResponse response) throws Exception {
		XSSFWorkbook workbook = new XSSFWorkbook();
		try {
			XSSFSheet sheet = workbook.createSheet("信息表");
			List<Map<String, Object>> engineList = keywordGMapper.getEngineList(); // 搜索引擎配置
			List<Map<String, Object>> keyWordExportList = keywordGMemberEMapper.getExprotKeyWordList(pam); // 导出数据
			String fileName = "关键词列表-" + DateUtil.getDay() + ".xlsx";// 设置要导出的文件的名字
			// 新增数据行，并且设置单元格数据

			int rowNum = 1;
			String[] headers = { "关键词", "域名", "搜索引擎", "当前排名", "使用状态" };
			// headers表示excel表中第一行的表头

			XSSFRow row = sheet.createRow(0);
			// 在excel表中添加表头

			for (int i = 0; i < headers.length; i++) {
				XSSFCell cell = row.createCell(i);
				XSSFRichTextString text = new XSSFRichTextString(headers[i]);
				cell.setCellValue(text);
			}
			Map<String, Object> keyWordExportMap = null;
			// 在表中存放查询到的数据放入对应的列
			for (int i = 0; i < keyWordExportList.size(); i++) {
				XSSFRow row1 = sheet.createRow(rowNum);
				keyWordExportMap = keyWordExportList.get(i);
				String domainType = (String) keyWordExportMap.get("domain_type");
				String domain = domainType + (String) keyWordExportMap.get("domain");
				String engineCode = (String) keyWordExportMap.get("engineCode");
				String engineStr = null;
				for (int j = 0; j < engineList.size(); j++) {
					Map<String, Object> engineMap = engineList.get(j);
					String code = (String) engineMap.get("code");
					if (code.equals(engineCode)) {
						engineStr = (String) engineMap.get("name");// 搜索引擎
						break;
					}
				}
				String state = (String) keyWordExportMap.get("state");
				String stateStr = null;
				if ("02".equals(state)) {
					stateStr = "已停用";
				} else if ("00".equals(state)) {
					stateStr = "待审核";
				} else {
					stateStr = "使用中";
				}
				row1.createCell(0).setCellValue(keyWordExportMap.get("keyWordName").toString());
				row1.createCell(1).setCellValue(domain);
				row1.createCell(2).setCellValue(engineStr);
				row1.createCell(3).setCellValue(keyWordExportMap.get("newRanking").toString());
				row1.createCell(4).setCellValue(stateStr);
				rowNum++;
			}
			response.setContentType("application/octet-stream");
			response.setHeader("Content-disposition",
					"attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));
			response.flushBuffer();
			workbook.write(response.getOutputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return workbook;
	}
	
	

}
